package com.threeti.mecool.core.util;

import java.math.BigDecimal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BigDecimalUtil {
  public static final Logger logger = LoggerFactory.getLogger(BigDecimalUtil.class);
  
  public static String percentFormattedValueOf(int aSmallerOrEqual, int bGreaterOrEqual) {
    int result = percentIntValueOf(aSmallerOrEqual, bGreaterOrEqual);
    if (result == 0) {
      return "0%";
    } else if (result == -1) {
      return "";
    } else {
      return result + "%";
    }
  }
  
  public static int percentIntValueOf(int aSmallerOrEqual, int bGreaterOrEqual) {
    if (bGreaterOrEqual == 0) {
      return 0;
    }
    if (aSmallerOrEqual > bGreaterOrEqual) {
      logger.warn("错误的调用，参数a不能大于b", new int[]{aSmallerOrEqual, bGreaterOrEqual});
      return -1;
    }
    double aDouble = (double)aSmallerOrEqual;
    double bDouble = (double)bGreaterOrEqual;
    BigDecimal result = BigDecimal.valueOf(aDouble/bDouble).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(1), 0, BigDecimal.ROUND_HALF_UP);
    return result.intValue();
  }
}
